package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.system.domain.ZtfVideoTable;
import com.ruoyi.system.mapper.ZtfVideoTableMapper;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZtfLearningRecordMapper;
import com.ruoyi.system.domain.ZtfLearningRecord;
import com.ruoyi.system.service.IZtfLearningRecordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-06-06
 */
@Service
public class ZtfLearningRecordServiceImpl implements IZtfLearningRecordService 
{
    @Autowired
    private ZtfLearningRecordMapper ztfLearningRecordMapper;

    @Autowired
    private ZtfVideoTableMapper ztfVideoTableMapper;
    /**
     * 查询课程记录
     * 
     * @param uuid 课程记录主键
     * @return 课程记录
     */
    @Override
    public ZtfLearningRecord selectZtfLearningRecordByUuid(String uuid)
    {
        return ztfLearningRecordMapper.selectZtfLearningRecordByUuid(uuid);
    }

    /**
     * 查询课程记录列表
     * 
     * @param ztfLearningRecord 课程记录
     * @return 课程记录
     */
    @Override
    @Transactional
    public List<ZtfLearningRecord> selectZtfLearningRecordList(ZtfLearningRecord ztfLearningRecord)
    {

        //计算学习时长
        List<ZtfLearningRecord> recordList = ztfLearningRecordMapper.selectZtfLearningRecordList(ztfLearningRecord);
//        for (ZtfLearningRecord learningRecord : recordList) {
//            if(learningRecord.getVideoId()!=null){
//                ZtfVideoTable ztfVideoTable= ztfVideoTableMapper.selectZtfVideoTableById(learningRecord.getVideoId());
//                //获取视频的总时长
//                if(ztfVideoTable!=null){
//                    String totalHourStr=ztfVideoTable.getDuration();
//                    //获取已经播放的时长
//                    String startTimeStr= learningRecord.getStartTime();
//                    if(totalHourStr!=null && startTimeStr!=null ){
//                        int totalHour=Integer.parseInt(totalHourStr);
//                        int startTime=Integer.parseInt(startTimeStr);
//                        //播放进度计算
//                        int video_rate=(totalHour-startTime)/totalHour * 100;
//                        //取整
//                        int roundedPercentage = (int) Math.round(video_rate);
//                        String video_rateStr = String.valueOf(roundedPercentage);
//                        ztfVideoTable.setViews(video_rateStr);
//                        ztfVideoTableMapper.updateZtfVideoTable(ztfVideoTable);
//                    }
//                }
//            }
//        }
        return recordList;
    }

    /**
     * 新增课程记录
     * 
     * @param ztfLearningRecord 课程记录
     * @return 结果
     */
    @Override
    public int insertZtfLearningRecord(ZtfLearningRecord ztfLearningRecord) {
        ZtfVideoTable ztfVideoTable = ztfVideoTableMapper.selectZtfVideoTableById(ztfLearningRecord.getVideoId());
        //获取视频的总时长
        if (ztfVideoTable != null) {
            String totalHourStr = ztfVideoTable.getDuration();
            //获取已经播放的时长
            String startTimeStr = ztfLearningRecord.getStartTime();
            if (totalHourStr != null && startTimeStr != null) {
                int totalHour = Integer.parseInt(totalHourStr);
                Float startTime = Float.parseFloat(startTimeStr);
                //播放进度计算
                Float video_rate = (totalHour - startTime) / totalHour * 100;
                //取整
                int roundedPercentage = (int) Math.floor(video_rate);
                String video_rateStr = String.valueOf(roundedPercentage);
                ztfVideoTable.setViews(video_rateStr);
                ztfVideoTable.setIsOver(ztfLearningRecord.getIsOver());
                ztfVideoTableMapper.updateZtfVideoTable(ztfVideoTable);

            }
        }
        //设置创建时间
        ztfLearningRecord.setCreateTime(new Date());
        //设置修改时间
        ztfLearningRecord.setUpdateTime(new Date());
        return ztfLearningRecordMapper.insertZtfLearningRecord(ztfLearningRecord);
    }

    /**
     * 修改课程记录
     * 
     * @param ztfLearningRecord 课程记录
     * @return 结果
     */
    @Override
    public int updateZtfLearningRecord(ZtfLearningRecord ztfLearningRecord)
    {
        return ztfLearningRecordMapper.updateZtfLearningRecord(ztfLearningRecord);
    }

    /**
     * 批量删除课程记录
     * 
     * @param uuids 需要删除的课程记录主键
     * @return 结果
     */
    @Override
    public int deleteZtfLearningRecordByUuids(String[] uuids)
    {
        return ztfLearningRecordMapper.deleteZtfLearningRecordByUuids(uuids);
    }

    /**
     * 删除课程记录信息
     * 
     * @param uuid 课程记录主键
     * @return 结果
     */
    @Override
    public int deleteZtfLearningRecordByUuid(String uuid)
    {
        return ztfLearningRecordMapper.deleteZtfLearningRecordByUuid(uuid);
    }
}
